/**
 * 
 */
var seckill ={
	
	ProjectPath : {
		local : function(){
		//获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp  
	    var curWwwPath=window.document.location.href;  
	    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp  
	    var pathName=window.document.location.pathname;  
	    var pos=curWwwPath.indexOf(pathName);  
	    //获取主机地址，如： http://localhost:8083  
	    var localhostPaht=curWwwPath.substring(0,pos);  
	    //获取带"/"的项目名，如：/uimcardprj  
	    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);  
	    return(localhostPaht+projectName); 
		}
	},
	URL : {
			now : function(){
				var path = seckill.ProjectPath.local();
				return seckill.ProjectPath.local()+'/seckill/time/now';
			},
			exposer : function(seckillId){
				return seckill.ProjectPath.local()+'/seckill/'+seckillId+'/exposer';
			},
			execution : function(seckillId,md5){
				return seckill.ProjectPath.local()+'/seckill/'+seckillId+'/'+md5+'/excution';
			},
	},
	validatePhone : function(phone){
		if(phone && phone.length == 11 && !isNaN(phone)){
			return true;
		} else {
			return false;
		}
	},
	handleSeckill : function(seckillId,node){
		node.hide().html('<button class="btn btn-primary btn-lg" id="killBtn"> 开始秒杀 </button> ');
		$.post(seckill.URL.exposer(seckillId),{},function(result){
			//回调函数中 执行交互
			if(result && result['success']){
				var exposer = result['data'];
				if(exposer['exposed']){
					//开启秒杀
					//获取秒杀地址
					var md5 = exposer['md5'];
					var killUrl = seckill.URL.execution(seckillId,md5);
					console.log("killUrl"+killUrl);
					//绑定一次点击事件
					$('#killBtn').one('click',function(){
						//绑定执行秒杀 请求
						$(this).addClass('disabled');
						$.post(killUrl,{},function(result){
							if(result && result['success']){
								var killResult = result['data'];
								var state = killResult['state'];
								var stateInfo = killResult['stateInfo'];
								//显示秒杀结果
								node.html('<span class="label label-success">' + stateInfo + '</span>')
							}
						});
					});
					node.show();
				}else{
					var now = exposer['now'];
					var start = exposer['start'];
					var end = exposer['end'];
				seckill.mycountdown(seckillId,now,start,end);	
				}
			}else{
				console.log('result:'+result);
			}
		})
	},
	mycountdown : function(seckillId,nowTime,startTime,endTime){
		var seckillBox = $('#seckill-box');
		//事件判断
		if(nowTime > endTime){
			//秒杀结束
			seckillBox.html('秒杀结束');
		}else if(nowTime < startTime){
			seckillBox.html('秒杀未开始111');
			//计时事件绑定
			var killTime = new Date(startTime+1000);
			seckillBox.countdown(killTime,function(event){
				var format = event.strftime('秒杀计时：%D天 %H时 %M分 %S秒');
				seckillBox.html(format);
			}).on('finish.countdown',function(){
				//获取秒杀地址
				seckill.handleSeckill(seckillId,seckillBox);
			});
			
		}else{
			//秒杀开始
			seckill.handleSeckill(seckillId,seckillBox);
		}
	},
	detail :{
		init : function(params){
			//手机验证和登录，计时交互
			//在cookie 中查询手机号
			var killPhone = $.cookie('killPhone');
			
			//验证手机号
			if(!seckill.validatePhone(killPhone)){
				var killPhoneModal = $('#killPhoneModal');
				//显示弹出层
				killPhoneModal.modal({
					show : true,
					backdrop : 'static',//禁止位置关闭
					keyboard : 'false' //关闭键盘事件
				});
				$('#killPhoneBtn').click(function(){
					var inputPhone = $('#killPhoneKey').val();
					if(seckill.validatePhone(inputPhone)){
						window.location.reload();
						//电话写入cookie
						$.cookie('killPhone',inputPhone,{expires : 7,path : '/seckill'});
					} else{
						$('#killPhoneMessage').hide().html('<label class="label label-danger">手机号错误</label>').show(300)
					}
				});
			}
			var seckillId = params['seckillId'];
			var startTime = params['startTime'];
			var endTime =params['endTime'];
			var urlTime = seckill.URL.now();
			$.get(seckill.URL.now(),{},function(result){
				if(result && result['success']){
					var nowTime = result['data'];
					seckill.mycountdown(seckillId,nowTime,startTime,endTime)
				}else{
					console.log('result:'+result);
				}
			});
		},
	}	
}